package cc.rengu.igas.channel.nucc.core.service.trans;

import cc.rengu.igas.channel.nucc.common.constant.NuccParamConstant;
import cc.rengu.igas.channel.nucc.common.constant.NuccTreeNodeConstant;
import cc.rengu.igas.channel.nucc.common.constant.NuccTxnNumConstant;
import cc.rengu.igas.channel.nucc.common.enums.RespCodeEnum;
import cc.rengu.igas.channel.nucc.common.enums.SignTypeEnum;
import cc.rengu.igas.channel.nucc.common.enums.TrxCtgyCdEnum;
import cc.rengu.igas.channel.nucc.core.realize.NuccSeqNoManage;
import cc.rengu.igas.channel.nucc.core.realize.NuccService;
import cc.rengu.igas.channel.nucc.core.realize.impl.NuccSeqNoManageImpl;
import cc.rengu.igas.channel.nucc.core.realize.impl.NuccServiceImpl;
import cc.rengu.igas.channel.nucc.facade.bean.*;
import cc.rengu.igas.channel.nucc.facade.request.AgreementPayAcqRequest;
import cc.rengu.igas.channel.nucc.facade.request.AgreementPayRequest;
import cc.rengu.igas.channel.nucc.facade.response.CommonTransAcqResponse;
import cc.rengu.igas.channel.nucc.facade.response.CommonTransResponse;
import cc.rengu.igas.channel.nucc.facade.request.AgreementPayRequest;
import cc.rengu.oltp.service.base.BizBaseService;
import cc.rengu.oltp.service.common.constant.AppParamConstant;
import cc.rengu.oltp.service.common.dao.SignAcctInfoMapper;
import cc.rengu.oltp.service.common.dao.impl.SignAcctInfoMapperImpl;
import cc.rengu.oltp.service.common.entity.SignAcctInfo;
import cc.rengu.oltp.service.model.RiskMonitorInfo;
import cc.rengu.oltp.service.common.constant.TreeNodeConstant;
import cc.rengu.oltp.service.model.BizException;
import cc.rengu.oltp.service.model.BizResponse;
import cc.rengu.oltp.utility.annotation.BIZ_PROCESS;
import cc.rengu.oltp.utility.util.*;
import cc.rengu.utility.base.TimeUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;

/**
 * 协议支付服务
 */
public class EpccAgreementPayService extends BizBaseService {
    @Override
    protected Object beforeBizProcess() throws Exception {
        XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();
        AgreementPayRequest request = new AgreementPayRequest();
        ConvertUtil.convertOutput(request);

        xmlTreeUtil.setXmlTreeStringValue(TreeNodeConstant.FRONT_SEQ_NUM, request.getHeader().getTraceNo());
        xmlTreeUtil.setXmlTreeStringValue(TreeNodeConstant.MCHNT_ORDER_ID, request.getOrderId());
        xmlTreeUtil.setXmlTreeStringValue(TreeNodeConstant.ORDER_CCY, request.getTrxAmt().substring(0, 3));
        xmlTreeUtil.setXmlTreeStringValue(TreeNodeConstant.ORDER_AMT, request.getTrxAmt().substring(3));
        xmlTreeUtil.setXmlTreeStringValue(TreeNodeConstant.PAY_ACCT_TYPE, request.getPyerAcctTp());
        return request;
    }

    @Override
    protected BizResponse callBizService(Object request) throws Exception {
        XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();
        AgreementPayRequest innerRequest = (AgreementPayRequest) request;
        xmlTreeUtil.setXmlTreeStringValue(NuccTreeNodeConstant.EPCC_ISSR_ID, innerRequest.getInstgId());

        NuccService nuccService = new NuccServiceImpl();
        AgreementPayAcqRequest acqRequest = initAgreementPayAcqRequest(xmlTreeUtil, innerRequest);

        CommonTransAcqResponse acqResponse = nuccService.agreementPay(acqRequest);
        BizResponse bizResponse = new BizResponse();
        bizResponse.setRspSysId(NuccParamConstant.IGAS_SYS_ID);
        if (!NuccParamConstant.EPCC_SUCCESS_CODE.equals(acqResponse.getSysRtnCd())) {
            rglog.error("调用网联接口失败，sysRtnCd=[{}],sysRtnDesc=[{}]", acqResponse.getSysRtnCd(), acqResponse.getSysRtnDesc());
            bizResponse.setRespCode(acqResponse.getSysRtnCd());
            bizResponse.setRespDesc(acqResponse.getSysRtnDesc());
        } else {
            BizInfBean bizInf = new BizInfBean();
            bizInf.setDbtrBankId(acqResponse.getDbtrBankId());
            bizInf.setCdtrBankId(acqResponse.getCdtrBankId());
            bizInf.setTrxStatus(acqResponse.getTrxStatus());
            bizInf.setTrxFinishTm(acqResponse.getTrxFinishTm());
            bizInf.setBatchId(acqResponse.getBatchId());
            /*将交易批次号保存到流水表中*/
            xmlTreeUtil.setXmlTreeStringValue(TreeNodeConstant.CHANNEL_RECON_KEY, acqResponse.getBatchId());

            CommonTransResponse response = new CommonTransResponse();
            response.setBizInf(bizInf);

            bizResponse.setRespCode(acqResponse.getBizStsCd());
            bizResponse.setRespDesc(acqResponse.getBizStsDesc());
            bizResponse.setResult(response);
        }

        return bizResponse;
    }

    @Override
    protected void afterBizProcess(BizResponse response) throws Exception {

    }

    @Override
    protected RiskMonitorInfo getRiskMonitorInfo() {
        return null;
    }

    private AgreementPayAcqRequest initAgreementPayAcqRequest(XmlTreeUtil xmlTreeUtil, AgreementPayRequest innerRequest) throws Exception {
        AgreementPayAcqRequest agreementPayAcqRequest = new AgreementPayAcqRequest();

        String signNo = innerRequest.getSignNo();
        PyerInfBean pyerInf = new PyerInfBean();
        pyerInf.setPyerAcctIssrId(innerRequest.getPyerAcctIssrId());
        pyerInf.setPyerAcctTp(innerRequest.getPyerAcctTp());
        pyerInf.setSgnNo(signNo);
        pyerInf.setPyerTrxTrmTp(innerRequest.getPyerTrxTrmTp());
        pyerInf.setPyerTrxTrmNo(innerRequest.getPyerTrxTrmNo());
        agreementPayAcqRequest.setPyerInf(pyerInf);

        PyeeInfBean pyeeInf = new PyeeInfBean();
        pyeeInf.setPyeeAcctIssrId(innerRequest.getPyeeAcctIssrId());
        pyeeInf.setPyeeAcctTp(innerRequest.getPyeeAcctTp());
        pyeeInf.setPyeeAcctId(innerRequest.getPyeeAcctId());
        pyeeInf.setPyeeNm(innerRequest.getPyeeNm());
        pyeeInf.setPyeeCtryNo(innerRequest.getPyeeCtryNo());
        pyeeInf.setPyeeAreaNo(innerRequest.getPyeeAreaNo());
        pyeeInf.setPyeeTrxTrmTp(innerRequest.getPyeeTrxTrmTp());
        pyeeInf.setPyeeTrxTrmNo(innerRequest.getPyeeTrxTrmNo());
        agreementPayAcqRequest.setPyeeInf(pyeeInf);
        agreementPayAcqRequest.setTrxDevcInf(innerRequest.getTrxDevcInf());

        agreementPayAcqRequest.setResfdAcctIssrId(innerRequest.getResfdAcctIssrId());
        agreementPayAcqRequest.setInstgAcctId(innerRequest.getInstgAcctId());
        agreementPayAcqRequest.setInstgAcctNm(innerRequest.getInstgAcctNm());

        agreementPayAcqRequest.setTrxCtgy(TrxCtgyCdEnum.SIGN_PAY.getType());
        agreementPayAcqRequest.setBizTp(innerRequest.getBizTp());
        /*获取网联流水号*/
        NuccSeqNoManage NuccSeqNoManage = new NuccSeqNoManageImpl();
        String epccTxnId = NuccSeqNoManage.genEpccSeqNo();
        agreementPayAcqRequest.setTrxId(epccTxnId);
        /*金融交易流水表没有通道流水字段，用remark1来保存*/
        xmlTreeUtil.setXmlTreeStringValue(TreeNodeConstant.REMARK1, epccTxnId);
        xmlTreeUtil.setXmlTreeStringValue(TreeNodeConstant.CHANNEL_SEQ_NUM, epccTxnId);
        agreementPayAcqRequest.setTrxDtTm(TimeUtil.getDateTime("yyyy-MM-dd'T'HH:mm:ss"));
        agreementPayAcqRequest.setTrxAmt(innerRequest.getTrxAmt());
        agreementPayAcqRequest.setAcctInTp(innerRequest.getAcctInTp());

        OrdrInfBean ordrInf = new OrdrInfBean();
        ordrInf.setOrderId(innerRequest.getOrderId());
        ordrInf.setOrderDesc(innerRequest.getOrderDetails());
        ordrInf.setPlatformName(innerRequest.getPltfrmNm());
        agreementPayAcqRequest.setOrdrInf(ordrInf);
        agreementPayAcqRequest.setTrxDevcInf(innerRequest.getTrxDevcInf());

        return agreementPayAcqRequest;
    }

}
